﻿using LedictCoreAuthorityLib;
using Microsoft.Web.Administration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AuthorityImport
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Authority auth = new Authority();
            txtHDCode.Text = auth.GetHardwareCode();
            string path = auth.GetShowRightInfo();
            string str = File.ReadAllText(path);
            BindTreeView(treeView1, str);
        }

        private void StopSite(ApplicationPool pool)
        {
            try
            {
                if (pool.State != ObjectState.Stopped)
                {
                    pool.Stop();
                    System.Threading.Thread.Sleep(1000);
                    if (pool.State != ObjectState.Stopped)
                    {
                        System.Threading.Thread.Sleep(1000);
                        pool.Stop();
                    }
                }
            }
            catch (Exception)
            {
                StopSite(pool);
            }
        }

        private void StartSite(ApplicationPool pool)
        {
            try
            {
                if (pool.State != ObjectState.Started)
                {
                    pool.Start();
                    System.Threading.Thread.Sleep(1000);
                    if (pool.State != ObjectState.Started)
                    {
                        System.Threading.Thread.Sleep(1000);
                        pool.Start();
                    }
                }
            }
            catch (Exception)
            {
                StartSite(pool);
            }
        }

        private void RestartSite()
        {
            ServerManager sm = new ServerManager();
            var pool = sm.ApplicationPools["LDT.JudgetDoc.shouquan"];
            StopSite(pool);
            StartSite(pool);           
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.ldt)|*.ldt";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                Authority auth = new Authority();
                if (auth.ImportFile(file))
                {
                    RestartSite();
                    MessageBox.Show("导入成功！");
                    string path = auth.GetShowRightInfo();
                    string str = File.ReadAllText(path);
                    BindTreeView(treeView1, str);
                }
                else
                {
                    MessageBox.Show("导入失败！");
                }
                
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定删除吗？", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Authority auth = new Authority();
                if (auth.DeleteRight())
                {
                    RestartSite();
                    MessageBox.Show("删除成功！");
                    treeView1.Nodes.Clear();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServerManager sm = new ServerManager();
            var pool = sm.ApplicationPools["LDT.JudgetDoc.shouquan"];
            if (pool != null)
            {
                if (pool.State != ObjectState.Stopped)
                {
                    pool.Stop();
                    System.Threading.Thread.Sleep(1000);
                    if (pool.State != ObjectState.Stopped)
                    {
                        System.Threading.Thread.Sleep(1000);
                        pool.Stop();
                    }
                }                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ServerManager sm = new ServerManager();
            var pool = sm.ApplicationPools["LDT.JudgetDoc.shouquan"];
            if (pool != null)
            {
                if (pool.State != ObjectState.Started)
                {
                    pool.Start();
                    System.Threading.Thread.Sleep(1000);
                    if (pool.State != ObjectState.Started)
                    {
                        System.Threading.Thread.Sleep(1000);
                        pool.Start();
                    }
                }
            }
        }

        /// <summary>
        /// 绑定树形控件
        /// </summary>
        /// <param name="treeView"></param>
        /// <param name="strJson"></param>
        public void BindTreeView(TreeView treeView, string strJson)
        {
            treeView.Nodes.Clear();

            if (IsJOjbect(strJson))
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);

                foreach (var item in jo)
                {
                    TreeNode tree;
                    if (item.Value.GetType() == typeof(JObject))
                    {
                        tree = new TreeNode(item.Key);
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        treeView.Nodes.Add(tree);
                    }
                    else if (item.Value.GetType() == typeof(JArray))
                    {
                        tree = new TreeNode(item.Key);
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        treeView.Nodes.Add(tree);
                    }
                    else
                    {
                        tree = new TreeNode(item.Key + ":" + item.Value.ToString());
                        treeView.Nodes.Add(tree);
                    }
                }
            }
            if (IsJArray(strJson))
            {
                JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
                int i = 0;
                foreach (JObject item in ja)
                {
                    TreeNode tree = new TreeNode();
                    foreach (var itemOb in item)
                    {
                        TreeNode treeOb;
                        if (itemOb.Value.GetType() == typeof(JObject))
                        {
                            treeOb = new TreeNode(itemOb.Key);
                            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);

                        }
                        else if (itemOb.Value.GetType() == typeof(JArray))
                        {
                            treeOb = new TreeNode(itemOb.Key);
                            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);
                        }
                        else
                        {
                            treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);
                        }
                    }
                    treeView.Nodes.Add(tree);
                }
            }
            treeView.ExpandAll();
        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="parantNode"></param>
        /// <param name="value"></param>
        public void AddTreeChildNode(ref TreeNode parantNode, string value)
        {
            if (IsJOjbect(value))
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(value);
                foreach (var item in jo)
                {
                    TreeNode tree;
                    if (item.Value.GetType() == typeof(JObject))
                    {
                        tree = new TreeNode(item.Key);
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        parantNode.Nodes.Add(tree);
                    }
                    else if (item.Value.GetType() == typeof(JArray))
                    {
                        tree = new TreeNode(item.Key);
                        AddTreeChildNode(ref tree, item.Value.ToString());
                        parantNode.Nodes.Add(tree);
                    }
                    else
                    {
                        tree = new TreeNode(item.Key + ":" + item.Value.ToString());
                        parantNode.Nodes.Add(tree);
                    }
                }
            }
            if (IsJArray(value))
            {
                JArray ja = (JArray)JsonConvert.DeserializeObject(value);
                int i = 0;
                foreach (JObject item in ja)
                {
                    TreeNode tree = new TreeNode();
                    parantNode.Nodes.Add(tree);
                    foreach (var itemOb in item)
                    {
                        TreeNode treeOb;
                        if (itemOb.Value.GetType() == typeof(JObject))
                        {
                            treeOb = new TreeNode(itemOb.Key);
                            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);

                        }
                        else if (itemOb.Value.GetType() == typeof(JArray))
                        {
                            treeOb = new TreeNode(itemOb.Key);
                            AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);
                        }
                        else
                        {
                            treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
                            tree.Nodes.Add(treeOb);
                        }
                    }
                }
            }
        }
        /// <summary>
        ///  判断是否JOjbect类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool IsJOjbect(string value)
        {
            try
            {
                JObject ja = JObject.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 判断是否JArray类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool IsJArray(string value)
        {
            try
            {
                JArray ja = JArray.Parse(value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
}
